package winOrDie.graphicsInterface.GameMenu;



import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;

import winOrDie.Application.WoD_Application_Data;
import winOrDie.Configuration.WoD_Configuration;
import winOrDie.Game.WoD_Game;
import winOrDie.Game.WoD_Game_Manager;
import winOrDie.Net.Http.httpmsg.WoD_HttpMessage;

public class Composite_GameMenu 
extends Composite 
implements Composite_Main_Listener,
Composite_List_Games_Listener,
Composite_Join_Game_Listener,
Composite_Configuration_Listener,
Composite_New_Game_Listener
{

	/**
	 * StackLayout variables
	 */
	private WoD_Configuration wod_configuration = null;
	
	public WoD_Configuration getWod_configuration() {
		return wod_configuration;
	}

	public void setWod_configuration(WoD_Configuration wod_configuration) {
		this.wod_configuration = wod_configuration;
	}

	private Composite composite_StackLayout = null;
	private Composite[] stack_Composites = null;
	private StackLayout stack_layout = null;  
	
	private static class Stack_Layout_Layers  {
			private static int Composite_Main = 0;
			private static int Composite_List_Games = 1;
			private static int Composite_Join_Game = 2;
			private static int Composite_Configuration = 3;
			private static int Composite_New_Game = 4;
	};
		
	
	
	/**
	 * Sections composites
	 */
	private Composite_Main composite_Main = null;
	private Composite_List_Games composite_List_Games = null;
	private Composite_Join_Game composite_Join_Game = null;
	private Composite_Configuration compositeConfiguration = null;
	private Composite_New_Game compositeNewGame = null;
	
	
	public Composite_GameMenu(Composite parent, int style, WoD_Configuration wodconf) {
		super(parent, style);
		setWod_configuration(wodconf);
		initialize();
	}

	private void initialize() {
		
		setSize(new Point(400, 400));
		setLayout(new GridLayout());
		//get_Composite_Main();
		
		
		createStackLayout();
		

		
	}

	/**
	 * This private method create stack layout
	 */
	private void createStackLayout() {
		composite_StackLayout = new Composite(this, SWT.NONE);
		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = SWT.FILL;
		gridData.verticalAlignment = SWT.FILL;
		
		
		
		
		stack_layout = new StackLayout ();
		composite_StackLayout.setLayout(stack_layout);
		composite_StackLayout.setLayoutData(gridData);
		
		
		/**
		 * Add composites to stack
		 */
		stack_Composites = new Composite[5];
		stack_Composites[Stack_Layout_Layers.Composite_Main] = get_Composite_Main();
		stack_Composites[Stack_Layout_Layers.Composite_List_Games] = get_composite_List_Games();
		stack_Composites[Stack_Layout_Layers.Composite_Join_Game] = get_Composite_Join_Game();
		stack_Composites[Stack_Layout_Layers.Composite_Configuration] = get_Composite_Configuration();
		stack_Composites[Stack_Layout_Layers.Composite_New_Game] = get_Composite_New_Game();
		
		stack_layout.topControl = stack_Composites[Stack_Layout_Layers.Composite_Main];
		
	}
	
	
	public void show_Main()
	{
		stack_layout.topControl = stack_Composites[Stack_Layout_Layers.Composite_Main];
		composite_StackLayout.layout();
	}
	
	/**
	 * This private method returns {@link Composite_Main} object
	 * @return {@link Composite_Main}
	 */
	public Composite_Main get_Composite_Main()
	{
		if (composite_Main == null)
		{
			GridData gridData_Composite_Main = new GridData();
			gridData_Composite_Main.grabExcessHorizontalSpace = true;
			gridData_Composite_Main.grabExcessVerticalSpace = true;
			gridData_Composite_Main.horizontalAlignment = SWT.CENTER;
			gridData_Composite_Main.verticalAlignment = SWT.CENTER;
			
			composite_Main = new Composite_Main(composite_StackLayout, SWT.NONE);
			composite_Main.setLayoutData(gridData_Composite_Main);
			
			/**
			 * Add this class as listener
			 */
			composite_Main.add_Composite_Main_Listener(this);

		}
		
		
		return composite_Main;
	}
	
	
	public void onComposite_Main_Change(Composite_Main_Event event) {
		
		switch (event.getEventType()) {
		case Composite_Main_Event.Event_Types.LIST_GAMES:
			stack_layout.topControl = stack_Composites[Stack_Layout_Layers.Composite_List_Games];
			composite_StackLayout.layout();
			try {
					composite_List_Games.Paint_Table(WoD_HttpMessage.GetCurrentGames(wod_configuration.getWeb_Server(), wod_configuration.getWeb_Server_Port()));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					show_Error(e.getMessage());
				}
			break;
		case Composite_Main_Event.Event_Types.JOIN_GAME:
			stack_layout.topControl = stack_Composites[Stack_Layout_Layers.Composite_Join_Game];
			composite_StackLayout.layout();
			break;
		case Composite_Main_Event.Event_Types.OPTIONS:
			stack_layout.topControl = stack_Composites[Stack_Layout_Layers.Composite_Configuration];
			composite_StackLayout.layout();
			break;	
			
		case Composite_Main_Event.Event_Types.CREATE_GAME:
			stack_layout.topControl = stack_Composites[Stack_Layout_Layers.Composite_New_Game];
			
			composite_StackLayout.layout();
			break;				
			
		default:
			break;
		}
		
	}
	
	
	/**
	 * This private method returns {@link Composite_List_Games} object
	 * @return {@link Composite_List_Games}
	 */
	public Composite_List_Games get_composite_List_Games()
	{
		if (composite_List_Games == null)
		{
			GridData gridData = new GridData();
			gridData.grabExcessHorizontalSpace = true;
			gridData.grabExcessVerticalSpace = true;
			gridData.horizontalAlignment = SWT.FILL;
			gridData.verticalAlignment = SWT.FILL;
			
			composite_List_Games = new Composite_List_Games(composite_StackLayout, SWT.NONE);
			//composite_List_Games.set_Paint_Table(WoD_HttpMessage.GetCurrentGames(wod_configuration.getWeb_Server(), wod_configuration.getWeb_Server_Port()));
			
			composite_List_Games.setLayoutData(gridData);
			
			
			/**
			 * Add this class as listener
			 */
			composite_List_Games.add_Composite_List_Games_Listener(this);
			
			//composite_List_Games.set_Paint_Table(WoD_HttpMessage.GetCurrentGames("localhost", "8080"));
			
//			composite_List_Games.set_WoD_Configuration(wod_configuration);
		}
		
		return composite_List_Games;
	}
	
	public void on_Composite_List_Games_Changed(Composite_List_Games_Event event) {
		switch (event.getEventType()) {
		case Composite_List_Games_Event.Event_Types.CANCEL:
			show_Main();
			break;
			
		case Composite_List_Games_Event.Event_Types.LIST_GAMES:
			try {
					composite_List_Games.Paint_Table(WoD_HttpMessage.GetCurrentGames(wod_configuration.getWeb_Server(), wod_configuration.getWeb_Server_Port()));
				} catch (Exception e) {

					show_Error(e.getMessage());
				}
			break;
					

		default:
			break;
		}
		
	}
	
	
	/**
	 * This private method returns {@link Composite_Join_Game} object
	 * @return {@link Composite_Join_Game}
	 */
	public Composite_Join_Game get_Composite_Join_Game()
	{
		if (composite_Join_Game == null)
		{
			GridData gridData = new GridData();
			gridData.grabExcessHorizontalSpace = true;
			gridData.grabExcessVerticalSpace = true;
			gridData.horizontalAlignment = SWT.CENTER;
			gridData.verticalAlignment = SWT.CENTER;
			
			composite_Join_Game = new Composite_Join_Game(composite_StackLayout, SWT.NONE);
			composite_Join_Game.setLayoutData(gridData);
			
			composite_Join_Game.add_Composite_Join_Game_Listener(this);
		}
		return composite_Join_Game;
	}
	
	public void on_Composite_Join_Game_Changed(Composite_Join_Game_Event event) {
		
		switch (event.getEventType()) {
		case Composite_Join_Game_Event.Event_Types.CANCEL:
			show_Main();
			break;

		default:
			break;
		}
	}
	
	
	public Composite_Configuration get_Composite_Configuration()
	{
		if (compositeConfiguration == null)
		{
			GridData gridData = new GridData();
			gridData.grabExcessHorizontalSpace = true;
			gridData.grabExcessVerticalSpace = true;
			gridData.horizontalAlignment = SWT.CENTER;
			gridData.verticalAlignment = SWT.CENTER;
			
			compositeConfiguration = new Composite_Configuration(composite_StackLayout, SWT.NONE);
			compositeConfiguration.setLayoutData(gridData);
			
			compositeConfiguration.add_Composite_Configuration_Listener(this);
		}
		
		return compositeConfiguration;
	}
	
	@Override
	public void on_Composite_Configuration_Change(Composite_Configuration_Event event) {
	
		switch (event.getEventType()) {
		case Composite_Configuration_Event.Event_Types.CANCEL:
			show_Main();
			break;

		default:
			break;
		}
	}
	
	
	
	public Composite_New_Game get_Composite_New_Game()
	{
		if (compositeNewGame == null)
		{
			GridData gridData = new GridData();
			gridData.grabExcessHorizontalSpace = true;
			gridData.grabExcessVerticalSpace = true;
			gridData.horizontalAlignment = SWT.CENTER;
			gridData.verticalAlignment = SWT.CENTER;
			
			compositeNewGame = new Composite_New_Game(composite_StackLayout, SWT.NONE);
			compositeNewGame.setLayoutData(gridData);
			
			compositeNewGame.add_Composite_New_Game_Listener(this);
		}
		
		return compositeNewGame;
	}
	
	
	@Override
	public void onComposite_New_Game_Change(Composite_New_Game_Event event) {
		switch (event.getEventType()) {
		case Composite_New_Game_Event.Event_Types.CANCEL:
			show_Main();
			break;

		default:
			break;
		}
		
	}
	
	/**
	 * Shows error message
	 * @param error
	 */
	private void show_Error(String error)
	{
		MessageBox messageBox = new MessageBox(getShell(), SWT.ICON_ERROR | SWT.OK);
		messageBox.setText("Error");
		messageBox.setMessage(error);
		messageBox.open();
	}
	
}
